package com.design.working.dao.impl;

import com.design.working.dao.UserDAO;
import com.design.working.util.JDBCUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 * @author gavin
 * @date 2022/6/19 15:13
 */
public class UserDAOImpl implements UserDAO {

    /**
     * 更新学生密码
     * @param id 学号
     * @param password 新密码
     */
    @Override
    public void updateStudentPassword(Integer id, String password) {

        try {
            Connection connection = JDBCUtil.getConnection();
            PreparedStatement preparedStatement;
            String sql = "UPDATE student SET password = ? WHERE uid = ?";

            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, password);
            preparedStatement.setInt(2, id);
            preparedStatement.executeUpdate();
            JDBCUtil.close(preparedStatement, connection);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 更新辅导员密码
     * @param id 工号
     * @param password 新密码
     */
    @Override
    public void updateCounselorPassword(Integer id, String password) {

        try {
            Connection connection = JDBCUtil.getConnection();
            PreparedStatement preparedStatement;
            String sql = "UPDATE counselor SET password = ? WHERE uid = ?";

            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, password);
            preparedStatement.setInt(2, id);
            preparedStatement.executeUpdate();
            JDBCUtil.close(preparedStatement, connection);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 更新用工部门员工密码
     * @param id 工号
     * @param password 新密码
     */
    @Override
    public void updateDeptEmployeePassword(Integer id, String password) {

        try {
            Connection connection = JDBCUtil.getConnection();
            PreparedStatement preparedStatement;
            String sql = "UPDATE dept_employee SET password = ? WHERE uid = ?";

            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, password);
            preparedStatement.setInt(2, id);
            preparedStatement.executeUpdate();
            JDBCUtil.close(preparedStatement, connection);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 更新管理员密码
     * @param id 工号
     * @param password 新密码
     */
    @Override
    public void updateAdminPassword(Integer id, String password) {

        try {
            Connection connection = JDBCUtil.getConnection();
            PreparedStatement preparedStatement;
            String sql = "UPDATE admin SET password = ? WHERE uid = ?";

            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, password);
            preparedStatement.setInt(2, id);
            preparedStatement.executeUpdate();
            JDBCUtil.close(preparedStatement, connection);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
